package com.android.wineshop;

import java.util.ArrayList;

import com.android.adapter.Customer_Adapter;
import com.android.controller.Customer_Controller;
import com.android.entity.Customer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class CustomerScreen extends Activity{
	Customer_Adapter customer_adapter;
	ListView customer_list;
	EditText txtSearch;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customer);
        ArrayList<Customer> array_customer = Customer_Controller.getListCustomer();
        customer_adapter = new Customer_Adapter(getApplicationContext(), R.layout.customer	, array_customer);
        customer_list = (ListView)findViewById(R.id.customer_list);
        customer_list.setAdapter(customer_adapter);
        customer_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent customer_detail = new Intent(CustomerScreen.this, CustomerDetail.class);
				Bundle data = new Bundle();
				Customer cust = (Customer) customer_list.getItemAtPosition(arg2);
				String customer_name = cust.getCustomer_name();
				data.putString("cust_name", customer_name);
				
				customer_detail.putExtras(data);
				startActivity(customer_detail);
			}
		});
        
        txtSearch = (EditText)findViewById(R.id.edtSearch);
        txtSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				String keyWord = txtSearch.getText().toString();
				customer_adapter.getFilter().filter(keyWord);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// Do Nothing
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// Do Nothing
				
			}
		});
        
	}

}
